﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace HindernisGame
{
    public class menu
    {
        private Game1 game;
        private SpriteBatch spriteBatch;
        private KeyboardState currentState;
        private Rectangle mainFrame;
        private List<menuButton> mbs = new List<menuButton>();
        private Texture2D beenden, score, neu, optionen;
        private int menuCount, sperre = 0;

        public menu(Game1 game)
        {
            this.game = game;
            neu = game.Content.Load<Texture2D>("menu/Neues-Spiel");
            optionen = game.Content.Load<Texture2D>("menu/Option");
            score = game.Content.Load<Texture2D>("menu/Highscore");
            beenden = game.Content.Load<Texture2D>("menu/Beenden");
            mbs.Add(new menuButton(new Rectangle(100, 220, 433, 72), neu, 0, true));
            mbs.Add(new menuButton(new Rectangle(100, 320, 372, 72), score, 1, false));
            mbs.Add(new menuButton(new Rectangle(100, 420, 327, 72), optionen, 2, false));
            mbs.Add(new menuButton(new Rectangle(100, 520, 389, 72), beenden, 3, false));
        }
     
        public void LoadContent()
        {
            spriteBatch = new SpriteBatch(game.GraphicsDevice);
            mainFrame = new Rectangle(0, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
        }

        public void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                game.Exit();

            if (sperre > 0)
                sperre--;
            else
            {
                currentState = Keyboard.GetState();
                if (currentState.IsKeyDown(Keys.Down))
                {
                    menuCount++;
                    sperre = 10;
                    foreach (menuButton m in mbs)
                    {
                        m.selected = false;
                    }
                }
                if (currentState.IsKeyDown(Keys.Up))
                {
                    menuCount--;
                    sperre = 10;
                    foreach (menuButton m in mbs)
                    {
                        m.selected = false;
                    }
                }
                if (menuCount == -1)
                    menuCount = 3;
                if(currentState.IsKeyDown(Keys.Enter))
                {
                    sperre = 20;
                    switch(menuCount%4){
                        case (0)://Neues Spiel
                            game.startGame();
                            break;
                        case(1)://Highscore
                            game.setCurrentScreen("highscoreScreen");
                            break;
                        case (2)://Optionen
                            game.setCurrentScreen("optionScreen");
                            break;
                        case(3):// Spiel beenden
                            game.Exit();
                        break;
                    }
                }

            }
            foreach (menuButton m in mbs)
            {
                if (m.index == menuCount%4)
                    m.selected = true;
            }

        }

        public void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(game.background, mainFrame, Color.White);
            spriteBatch.Draw(game.logo, new Rectangle(100,-50,824,291), new Color(255,255,255,255));

                foreach (menuButton m in mbs)
                {
                    m.Draw(spriteBatch, game.font);
                }
            spriteBatch.End();
        }

    }
}